<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-9-06
 * Time: 下午22:31
 * To change this template use File | Settings | File Templates.
 */
class TrendsDB  extends DBReader{

    //根据trend_id 获取trend 信息
    public function get($trend_id){

        $sql = 'select trend_id, source_id, title, trend_type, link, user_id, update_time from trends where trend_id = :trend_id';

        return $this->db()->getRow($sql,array('trend_id'=>$trend_id));
    }

    //根据title 获取trend 信息
    public function getTrendInfoByTitle($title){

        $sql = 'select trend_id, source_id, title, trend_type, link, user_id, update_time from trends where title = :title';

        return $this->db()->getRow($sql,array('title'=>$title));
    }

    //获取摘要 列表<带分页>
    public function getTrendList($curr_page,$per_page,$filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['title'])){
            $where .= ' and title like :title';
            $bindarr['title'] = '%'.$filter['title'].'%';
        }

        if(isset($filter['trend_type'])){
            $where .= ' and trend_type = :trend_type';
            $bindarr['trend_type'] = $filter['trend_type'];
        }

        if(isset($filter['source_id'])){
            $where .= ' and source_id = :source_id';
            $bindarr['source_id'] = $filter['source_id'];
        }

        if(isset($filter['u_stime'])){
            $where .= ' and update_time >= :u_stime';
            $bindarr['u_stime'] = $filter['u_stime'];
        }

        if(isset($filter['u_etime'])){
            $where .= ' and update_time <= :u_etime';
            $bindarr['u_etime'] = $filter['u_etime'];
        }

        $ret = $this->pager($curr_page, $per_page)
            ->ct('select count(trend_id) from trends where ' . $where , $bindarr)
            ->rs('select trend_id, source_id, title, trend_type, link, user_id, update_time from trends where '.$where.' order by trend_id desc',$bindarr);

        return $ret;
    }

    //添加资讯摘要信息
    public function addTrend($source_id, $title, $trend_type = 1, $link, $user_id, $update_time = null){
        $sql = "insert into trends(source_id, title, trend_type, link, user_id, update_time) values(:source_id, :title, :trend_type, :link, :user_id, :update_time)";

        $bindarr = array(
            'source_id'=>$source_id,
            'title'=>$title,
            'trend_type'=>$trend_type,
            'link'=>$link,
            'update_time'=>empty($update_time) ? date('Y-m-d H:i:s') : $update_time,
            'user_id'=>$user_id,
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //删除摘要信息
    public function delTrend($trend_id){

        $sql = "delete from trends where trend_id = :trend_id";

        $bindarr = array(
            'trend_id'=>$trend_id,
        );
        $rs = $this->db()->execute($sql,$bindarr);

        return $rs->rowCount();
    }

    //添加资讯摘要信息
    public function editTrend($trend_id, $source_id, $title, $trend_type = 1, $link, $user_id, $update_time = null){
        $sql = "update trends set source_id = :source_id,title = :title,trend_type = :trend_type,link = :link,update_time = :update_time,user_id = :user_id where trend_id = :trend_id";
        $bindarr = array(
            'trend_id' => $trend_id,
            'source_id'=>$source_id,
            'title'=>$title,
            'trend_type'=>$trend_type,
            'link'=>$link,
            'update_time'=>empty($update_time) ? date('Y-m-d H:i:s') : $update_time,
            'user_id'=>$user_id
        );
        return $this->db()->execute($sql,$bindarr);
    }
}